home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / infodata / callbook.tar / callbook_1.3 / misc.c < prev    next >
C/C++ Source or Header  |  1991-03-31  |  3KB  |  184 lines

  1. /*
  2.  * Telnet callsign server sources copyright 1989 by Devon Bowen, KA2NRC.
  3.  * You may distribute and modify these files as you please as long as
  4.  * as long as credit to the original creator is given. Please report any
  5.  * bug fixes or modification to bowen@cs.buffalo.edu.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <syslog.h>
  11.  
  12. char *index();
  13.  
  14.  
  15. /*
  16.  * strhash - given a pointer to a string, hash it based on the
  17.  * upper case characters in the string and return an unsigned
  18.  * int in the range 0-65535.
  19.  */
  20.  
  21. unsigned short
  22. strhash(str)
  23. char *str;
  24. {
  25.     int hash = 0;
  26.  
  27.     for (; *str != '\0' && *str != '|'; str++) {
  28.         if (islower(*str))
  29.             hash = (hash * 29 + *str - 96) % 65536;
  30.         if (isupper(*str))
  31.             hash = (hash * 29 + *str - 64) % 65536;
  32.         if (isdigit(*str))
  33.             hash = (hash * 29 + *str - 48) % 65536;
  34.     }
  35.  
  36.     return((unsigned short) hash);
  37. }
  38.  
  39.  
  40. strcasecpy(to, from)
  41. char *to, *from;
  42. {
  43.     while (*from) {
  44.         if (islower(*from))
  45.             *to++ = toupper(*from);
  46.         else
  47.             *to++ = *from;
  48.  
  49.         from++;
  50.     }
  51.  
  52.     *to = '\0';
  53. }
  54.  
  55.  
  56. /*
  57.  * my_fgets - given the normal parameters to fgets, get the line
  58.  * and zero out the '\n' at the end of the string (if there is one).
  59.  */
  60.  
  61. char *
  62. my_fgets(buf, num, fd)
  63. char *buf;
  64. int num;
  65. FILE *fd;
  66. {
  67.     char *n;
  68.  
  69.     if (fgets(buf, num, fd)) {
  70.         if (n = index(buf, '\n'))
  71.             *n = '\0';
  72.         return(buf);
  73.     } else
  74.         return(NULL);
  75. }
  76.  
  77.  
  78. paramnum(str)
  79. char *str;
  80. {
  81.     int count=0;
  82.  
  83.     while (isspace(*str))
  84.         str++;
  85.  
  86.     while (*str) {
  87.  
  88.         if (*str == '"') {
  89.             str++;
  90.             while (*str && *str != '"')
  91.                 str++;
  92.  
  93.             if (*str)
  94.                 str++;
  95.             else
  96.                 return(-1);
  97.         } else
  98.  
  99.             while (*str && !isspace(*str))
  100.                 str++;
  101.  
  102.         while (isspace(*str))
  103.             str++;
  104.  
  105.         count++;
  106.     }
  107.  
  108.     return(count);
  109. }
  110.  
  111.  
  112. char *
  113. param(str, num)
  114. char *str;
  115. int num;
  116. {
  117.     static char buffer[BUFSIZ];
  118.     int bufcnt=0;
  119.  
  120.     while (isspace(*str))
  121.         str++;
  122.  
  123.     while (*str && num) {
  124.  
  125.         if (*str == '"') {
  126.             str++;
  127.             while (*str && *str != '"')
  128.                 str++;
  129.  
  130.             if (*str)
  131.                 str++;
  132.         } else
  133.             while (*str && !isspace(*str))
  134.                 str++;
  135.  
  136.         while (isspace(*str))
  137.             str++;
  138.  
  139.         num--;
  140.     }
  141.  
  142.     if (num || (*str == '\0'))
  143.         return(NULL);
  144.  
  145.     if (*str == '"') {
  146.         str++;
  147.         while (*str && *str != '"') {
  148.             buffer[bufcnt++] = *str;
  149.             str++;
  150.         }
  151.     } else
  152.         while (*str && !isspace(*str)) {
  153.             buffer[bufcnt++] = *str;
  154.             str++;
  155.         }
  156.  
  157.     buffer[bufcnt] = '\0';
  158.  
  159.     return(buffer);
  160. }
  161.  
  162.  
  163. stvalid(state)
  164. char *state;
  165. {
  166.     int count = 0;
  167.     static char *states[] = {
  168.  
  169.                "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE",
  170.                "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS",
  171.                "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
  172.                "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY",
  173.                "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
  174.                "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV",
  175.                "WI", "WY", "VI", NULL
  176.  
  177.              };
  178.  
  179.     while (states[count] && strcasecmp(states[count], state))
  180.         count++;
  181.  
  182.     return (states[count] ? 1 : 0);
  183. }
  184.